//
//  ByLanguageController.m
//  laserdevice_ios
//
//  Created by pro on 16/11/24.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByLanguageController.h"

// vc
#import "ByUIIndexVC.h"                             // 首页

@interface ByLanguageController ()

@property(nonatomic, strong) NSBundle *bundle;      // 语言文件库

@end

@implementation ByLanguageController

static ByLanguageController *instance;

+ (instancetype)shareInstance {
    
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        instance = [[ByLanguageController alloc] init];
    });
    return instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    
    if (instance == nil) {
        instance = [super allocWithZone:zone];
    }
    return instance;
}
- (instancetype)init {
    
    if (self = [super init]) {

        ByLanguageMode mode;
        if ([ByCommonCache getSystemLanguage]) {
            mode = [ByCommonCache getSystemLanguage].intValue;
        } else {
            mode = ByLanguageModeEnglish;
        }
        [self setMode:mode];
    }
    return self;
}

- (void)setMode:(ByLanguageMode)mode {
    
    _mode = mode;
    
    NSString *language;
    if (_mode == ByLanguageModeChinaese) {
        language = @"zh-Hans";
    } else {
        language = @"en";
    }
    // 获取语言文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    self.bundle = [NSBundle bundleWithPath:path];
}


- (NSString *)textForKey:(NSString *)key {
    
    return NSLocalizedStringFromTableInBundle(key, @"language", self.bundle, @"");
}

- (void)changeLanguage {
    
    // 改变底部视图
    ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:[[ByUIIndexVC alloc] init]];
    BY_APPDELEGATE.window.rootViewController = navi;
}




@end
